﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using WFNetLib.Forms;
using WFNetLib;
using Microsoft.Office.Interop.Excel;
using System.IO;
using WFNetLib.Strings;

namespace 桑老师大庆项目数据处理
{
    public partial class Form1 : Form
    {
        CommunicationDebugForm CommunicationDebugForm=null;
        byte[] rx1;
        byte[] rx2;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            cbCom.Items.Clear();
            cbCom.Items.AddRange(ports);
            cbCom.SelectedIndex = 0;
//             CommunicationDebugForm = new CommunicationDebugForm();
//             CommunicationDebugForm.Show();
            rx1 = new byte[550];
            rx2 = new byte[550];
            t = new ushort[150];
            p = new ushort[150];
        }
        int limit = 0;
        ushort[] t, p;
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            FileInfo f;
            if (openFileDialog1.ShowDialog() != DialogResult.OK)
                return;
            string str = File.ReadAllText(openFileDialog1.FileName);
            byte[] read;
            try
            {
                read = StringsFunction.strToHexByte(str, " ");
            }
            catch //(System.Exception ex)
            {
                MessageBox.Show("数据文件有误，无法导入");
                return;
            }
            if(read.Length<560)
            {
                MessageBox.Show("数据文件有误，无法导入");
                return;
            }
            byte[] data = new byte[245 * 2];
            for (int i = 0; i < 35; i++)
            {
                for (int j = 0; j < 14; j++)
                {
                    data[i * 14 + j] = read[i * 16 + 2 + j];
                }
            }
            for (int i = 0; i < 122; i++)
            {
                p[i] = BytesOP.MakeShort(data[i * 4 + 1], data[i * 4]);
                t[i] = BytesOP.MakeShort(data[i * 4 + 3], data[i * 4+2]);
            }
            MessageBox.Show("数据采集完成");
//             SerialPort _COM = null;
//             _COM = new SerialPort(cbCom.Text, 9600, Parity.None, 8, StopBits.One);
//             _COM.ReadTimeout = 50000;
//             while (true)
//             {
//                 _COM.Close();
//                 try
//                 {
//                     _COM.Open();
//                     break;
//                 }
//                 catch
//                 {
//                     if (MessageBox.Show("当前串口无法打开,是否重试", "通信错误", MessageBoxButtons.YesNo) == DialogResult.No)
//                         return;
//                 }
//             }
//             _COM.Write("a");
//             //CommunicationDebugForm.ExternShowTx("a");
//             byte[] rxList=new byte[1096];
//             for (int i = 0; i < 1096; i++)
//             {
//                 rxList[i] = (byte)_COM.ReadByte();
//             }
//             //CommunicationDebugForm.ExternShowRx(rxList);
//             _COM.Close();            
//             for (int i = 0; i < 550; i++)
//                 rx1[i] = rxList[i];
//             for (int i = 0; i < 16; i++)
//                 rx2[i] = rxList[i];
//             for(int i=0;i<534;i++)
//                 rx2[16+i]=rxList[562+i];
//             limit = 1;
//             ExcelExport ExcelEx;
//             ExcelEx = new ExcelExport();
//             ExcelEx.ExcelWorkbookCallbackProc = new ExcelExport.ExcelWorkbookCallback(ExcelWorkbookCallbackProc);
//             ExcelEx.ExcelExportProc();
//             limit = 2;
//             ExcelEx.ExcelExportProc();
        }
        bool ExcelWorkbookCallbackProc(Workbook wBook, int index)
        {
            Worksheet wSheet;            
            //wBook.Worksheets.Add();            
            Range dr;
            if (index == -1)
            {
                for (int i = 0; i < wBook.Worksheets.Count - 1; i++)
                {
                    wSheet = (Worksheet)wBook.Worksheets[i + 1];
                    wSheet.Delete();
                }
//                 wSheet = (Worksheet)wBook.Worksheets[1];//4
//                 wSheet.Name = "门限2去头数据";
//                 wSheet.Cells[1, 1] = "压力";
//                 wSheet.Cells[1, 2] = "温度";
//                 dr = wSheet.get_Range("A1", "B1");
//                 dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkOrange);
//                 dr.Interior.Pattern = XlPattern.xlPatternSolid;
//                 wBook.Worksheets.Add();
//                 wSheet = (Worksheet)wBook.Worksheets[1];//3
//                 wSheet.Name = "门限2原始数据";
//                 wSheet.Cells[1, 1] = "帧头";
//                 wSheet.Cells[1, 2] = "数据1";
//                 wSheet.Cells[1, 3] = "数据2";
//                 wSheet.Cells[1, 4] = "数据3";
//                 wSheet.Cells[1, 5] = "数据4";
//                 wSheet.Cells[1, 6] = "数据5";
//                 wSheet.Cells[1, 7] = "数据6";
//                 wSheet.Cells[1, 8] = "数据7";
//                 dr = wSheet.get_Range("A1", "H1");
//                 dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkOrange);
//                 dr.Interior.Pattern = XlPattern.xlPatternSolid;
//                 wBook.Worksheets.Add();
                /************************************************************************/
                /* 早期方法                                                             */
                /************************************************************************/
//                 wSheet = (Worksheet)wBook.Worksheets[1];//2
//                 wSheet.Name = "实验数据";
//                 if(limit==1)
//                     wSheet.Cells[1, 1] = "门限1实验数据";
//                 else
//                     wSheet.Cells[1, 1] = "门限2实验数据";
//                 wSheet.Cells[2, 1] = "生成时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
//                 wSheet.Cells[3, 1] = "压力";
//                 wSheet.Cells[3, 2] = "温度";
//                 dr = wSheet.get_Range("A3", "B3");
//                 dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkOrange);
//                 dr.Interior.Pattern = XlPattern.xlPatternSolid;
//                 wBook.Worksheets.Add();
//                 wSheet = (Worksheet)wBook.Worksheets[1];//1
//                 wSheet.Name = "原始数据";
//                 if(limit==1)
//                     wSheet.Cells[1, 1] = "门限1原始数据";
//                 else
//                     wSheet.Cells[1, 1] = "门限2原始数据";
//                 wSheet.Cells[2, 1] = "生成时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
//                 wSheet.Cells[3, 1] = "帧头";
//                 wSheet.Cells[3, 2] = "数据1";
//                 wSheet.Cells[3, 3] = "数据2";
//                 wSheet.Cells[3, 4] = "数据3";
//                 wSheet.Cells[3, 5] = "数据4";
//                 wSheet.Cells[3, 6] = "数据5";
//                 wSheet.Cells[3, 7] = "数据6";
//                 wSheet.Cells[3, 8] = "数据7";
//                 dr = wSheet.get_Range("A3", "H3");
//                 dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkOrange);
//                 dr.Interior.Pattern = XlPattern.xlPatternSolid;
//                 dr = wSheet.get_Range("A4", "H37");
//                 dr.NumberFormatLocal = "0000";
//                 dr.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
//                 dr = wSheet.get_Range("A38", "C38");
//                 dr.NumberFormatLocal = "0000";
//                 dr.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
//                 return true;
                /************************************************************************/
                /*                                                                      */
                /************************************************************************/
                wSheet = (Worksheet)wBook.Worksheets[1];//2
                wSheet.Name = "实验数据";                
                wSheet.Cells[1, 1] = "生成时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                wSheet.Cells[2, 1] = "压力";
                wSheet.Cells[2, 2] = "温度";
                dr = wSheet.get_Range("A2", "B2");
                dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkOrange);
                dr.Interior.Pattern = XlPattern.xlPatternSolid;

//                 dr = wSheet.get_Range("A3", "B124");
//                 dr.NumberFormatLocal = "0000";
//                 dr.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                return true;

            }
            else
            {
                wSheet = (Worksheet)wBook.Worksheets[1];//门限1原始数据
                for (int i = 0; i < 122; i++)
                {
                    wSheet.Cells[3 + i, 1] = p[i].ToString("");
                    wSheet.Cells[3 + i, 2] = t[i].ToString("");
                        
                }
                /************************************************************************/
                /* 早期方法                                                             */
                /************************************************************************/
//                 ushort[] yt = new ushort[240];
//                 if(limit==1)
//                 {
//                     for (int i = 0; i < 34; i++)
//                     {
//                         wSheet.Cells[4 + i, 1] = BytesOP.MakeShort(rx1[16 * i + 1], rx1[16 * i + 0]).ToString("X4");
//                         wSheet.Cells[4 + i, 2] = BytesOP.MakeShort(rx1[16 * i + 3], rx1[16 * i + 2]).ToString("X4");
//                         wSheet.Cells[4 + i, 3] = BytesOP.MakeShort(rx1[16 * i + 5], rx1[16 * i + 4]).ToString("X4");
//                         wSheet.Cells[4 + i, 4] = BytesOP.MakeShort(rx1[16 * i + 7], rx1[16 * i + 6]).ToString("X4");
//                         wSheet.Cells[4 + i, 5] = BytesOP.MakeShort(rx1[16 * i + 9], rx1[16 * i + 8]).ToString("X4");
//                         wSheet.Cells[4 + i, 6] = BytesOP.MakeShort(rx1[16 * i + 11], rx1[16 * i + 10]).ToString("X4");
//                         wSheet.Cells[4 + i, 7] = BytesOP.MakeShort(rx1[16 * i + 13], rx1[16 * i + 12]).ToString("X4");
//                         wSheet.Cells[4 + i, 8] = BytesOP.MakeShort(rx1[16 * i + 15], rx1[16 * i + 14]).ToString("X4");
//                         for (int j = 0; j < 7; j++)
//                             yt[7 * i + j] = BytesOP.MakeShort(rx1[16 * i + 2 + j * 2 + 1], rx1[16 * i + 2 + j * 2]);
//                     }
//                     wSheet.Cells[38, 1] = BytesOP.MakeShort(rx1[545], rx1[544]).ToString("X4");
//                     wSheet.Cells[38, 2] = BytesOP.MakeShort(rx1[547], rx1[546]).ToString("X4");
//                     wSheet.Cells[38, 3] = BytesOP.MakeShort(rx1[549], rx1[548]).ToString("X4");
//                     yt[238] = BytesOP.MakeShort(rx1[547], rx1[546]);
//                     yt[239] = BytesOP.MakeShort(rx1[549], rx1[548]);
//                 }
//                 else
//                 {
//                     for (int i = 0; i < 34; i++)
//                     {
//                         wSheet.Cells[4 + i, 1] = BytesOP.MakeShort(rx2[16 * i + 1], rx2[16 * i + 0]).ToString("X4");
//                         wSheet.Cells[4 + i, 2] = BytesOP.MakeShort(rx2[16 * i + 3], rx2[16 * i + 2]).ToString("X4");
//                         wSheet.Cells[4 + i, 3] = BytesOP.MakeShort(rx2[16 * i + 5], rx2[16 * i + 4]).ToString("X4");
//                         wSheet.Cells[4 + i, 4] = BytesOP.MakeShort(rx2[16 * i + 7], rx2[16 * i + 6]).ToString("X4");
//                         wSheet.Cells[4 + i, 5] = BytesOP.MakeShort(rx2[16 * i + 9], rx2[16 * i + 8]).ToString("X4");
//                         wSheet.Cells[4 + i, 6] = BytesOP.MakeShort(rx2[16 * i + 11], rx2[16 * i + 10]).ToString("X4");
//                         wSheet.Cells[4 + i, 7] = BytesOP.MakeShort(rx2[16 * i + 13], rx2[16 * i + 12]).ToString("X4");
//                         wSheet.Cells[4 + i, 8] = BytesOP.MakeShort(rx2[16 * i + 15], rx2[16 * i + 14]).ToString("X4");
//                         for (int j = 0; j < 7; j++)
//                             yt[7 * i + j] = BytesOP.MakeShort(rx2[16 * i + 2 + j * 2 + 1], rx2[16 * i + 2 + j * 2]);
//                     }
//                     wSheet.Cells[38, 1] = BytesOP.MakeShort(rx2[545], rx2[544]).ToString("X4");
//                     wSheet.Cells[38, 2] = BytesOP.MakeShort(rx2[547], rx2[546]).ToString("X4");
//                     wSheet.Cells[38, 3] = BytesOP.MakeShort(rx2[549], rx2[548]).ToString("X4");
//                     yt[238] = BytesOP.MakeShort(rx2[547], rx2[546]);
//                     yt[239] = BytesOP.MakeShort(rx2[549], rx2[548]);
//                 }
//                 wSheet = (Worksheet)wBook.Worksheets[2];//门限2原始数据
//                 for (int i = 0; i < 120; i++)
//                 {
//                     wSheet.Cells[4 + i, 1] = yt[i*2].ToString();
//                     wSheet.Cells[4 + i, 2] = yt[i*2+1].ToString();
//                 }
                /************************************************************************/
                /*                                                                      */
                /************************************************************************/
                return false;
            }            
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            ExcelImport excelIn=new ExcelImport(true,"实验数据");
            System.Data.DataTable dt= excelIn.ExcelImportProc_OleDB();
            chart2.Series[0].Points.Clear();
            chart1.Series[0].Points.Clear();
            for(int i=0;i<dt.Rows.Count-2;i++)
            {
                chart2.Series[0].Points.AddY(int.Parse(dt.Rows[i + 2][1].ToString()));
                chart1.Series[0].Points.AddY(int.Parse(dt.Rows[i + 2][0].ToString()));
            }            
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            limit = 1;
            ExcelExport ExcelEx;
            ExcelEx = new ExcelExport();
            ExcelEx.ExcelWorkbookCallbackProc = new ExcelExport.ExcelWorkbookCallback(ExcelWorkbookCallbackProc);
            ExcelEx.ExcelExportProc();
//             limit = 2;
//             ExcelEx.ExcelExportProc();
        }
    }
}
